package com.unidentifiable.wechat.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.unidentifiable.wechat.accesstoken.AccessToken;
import com.unidentifiable.wechat.accesstoken.AccessTokenInfo;
import com.unidentifiable.wechat.service.SubscribeService;
import com.unidentifiable.wechat.util.MessageUtil;
import java.io.InputStream;
import java.util.Properties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Controller
 * @author unidentifiable
 * @date 2021-02-28
 */
@Slf4j
@RestController
//@RequestMapping("/my")
public class SubscribeController {
    @Autowired
    private SubscribeService subscribeService;

    /**
     * 服务器校验
     * @param req 请求
     * @return 校验结果
     */
    @GetMapping("/")
    public String verification(HttpServletRequest req) {
        return subscribeService.verification(req);
    }

    /**
     * 消息事件
     * @param req 请求
     * @return 结果
     */
    @PostMapping("/")
    public boolean subscribeEvent(HttpServletRequest req) {
        log.info("微信消息事件开始，请求参数：");
        String s = subscribeService.subscribeEvent(req);
        return null != s;
    }

    @GetMapping("/test")
    @ResponseBody
    public Object test(){
        return AccessTokenInfo.accessToken;
    }

    @GetMapping("/token/update")
    public void updateToken(){
        Properties prop = new Properties();
        String appid = null;
        String secret = null;
        try {
            // 读取 application.yml 获取微信开发者 appid 和 appsecret
            InputStream in = MessageUtil.class.getClassLoader().getResourceAsStream("application.yml");
            prop.load(in);
            appid = prop.getProperty("appid");
            secret = prop.getProperty("secret");
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 接口地址为https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET，其中grant_type固定写为client_credential即可。
        String url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appid, secret);
        // 此请求为https的get请求，返回的数据格式为{"access_token":"ACCESS_TOKEN","expires_in":7200}
        String result = HttpUtil.get(url);

        System.out.println("获取到的access_token=" + result);

        //使用FastJson将Json字符串解析成Json对象
        JSONObject json = new JSONObject(result);
        AccessToken token = new AccessToken();
        token.setTokenName(json.get("access_token", String.class));
        token.setExpireSecond(json.get("expires_in", Integer.class));

        AccessTokenInfo.accessToken = token;
    }
}
